package interfaze;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import constants.GameTables;

import entities.Player;

import logic.Utilities;

@SuppressWarnings("serial")
public class SpaceInvadersHiScorePanel extends JPanel implements ActionListener, MouseListener, ItemListener {
	
	SuperArcade2in1 window;
	private JLabel background = new JLabel();
	private JTable tLeaderBoard = new JTable();
	private JPanel tablePanel = new JPanel();
	private JLabel lShow = new JLabel("Show:");
	private JCheckBox cbOnlyMines = new JCheckBox("Only Mine's");
	private ButtonGroup radiogroup = new ButtonGroup();
	private JRadioButton rbAllRecords = new JRadioButton("All records", false);
	private JRadioButton rbMaxRecords = new JRadioButton("Maximum records", true);
	private JLabel lSearch = new JLabel("First found by name:");
	private JTextField tfSearch = new JTextField();
	private JButton bSearch = new JButton("Search");
	private JButton bBack = new JButton("Back");
	private Font font;
	private DefaultTableModel model = new DefaultTableModel(0, 3);
	public SpaceInvadersHiScorePanel(SuperArcade2in1 window) {
		this.window = window;
		this.setLayout(null);
		
		font = new Font("Helvetica", Font.PLAIN, Math.round(12 * window.xScale));
		
		tablePanel.setBounds(Math.round(300 * window.xScale), Math.round(50 * window.yScale),
				Math.round(900 * window.xScale), Math.round(600 * window.yScale));
		tablePanel.setLayout(null);
		
		tLeaderBoard.setFont(font);
		tLeaderBoard.setShowGrid(true);
		tLeaderBoard.setShowHorizontalLines(true);
		tLeaderBoard.setShowVerticalLines(true);
		
		tablePanel.setLayout( new BorderLayout() );
		tablePanel.add( new JScrollPane(tLeaderBoard), BorderLayout.CENTER);
		
		model.setColumnIdentifiers(new String[] {"Position", "Nick", "Score"});
		loadModel(database.Search.playersMaxRecord(GameTables.SPACEINVADERS));
		tLeaderBoard.setModel(model);
		tLeaderBoard.getTableHeader().setFont(font);
		tLeaderBoard.getTableHeader().setReorderingAllowed(false);
		tLeaderBoard.getTableHeader().setEnabled(false);
		tLeaderBoard.setEnabled(false);
		
		lShow.setBounds(Math.round(25 * window.xScale), Math.round(50 * window.yScale),
				Math.round(50 * window.xScale), Math.round(25 * window.yScale));
		lShow.setFont(font);
		lShow.setForeground(Color.WHITE);
		
		cbOnlyMines.setBounds(Math.round(125 * window.xScale), Math.round(50 * window.yScale), 
				Math.round(110 * window.xScale), Math.round(25 * window.yScale));
		cbOnlyMines.setForeground(Color.WHITE);
		cbOnlyMines.setFont(font);
		cbOnlyMines.setOpaque(false);
		
		radiogroup.add(rbMaxRecords);
		radiogroup.add(rbAllRecords);
		
		rbMaxRecords.setBounds(Math.round(33 * window.xScale), Math.round(75 * window.yScale),
				Math.round(150 * window.xScale), Math.round(25 * window.yScale));
		rbMaxRecords.setFont(font);
		rbMaxRecords.setForeground(Color.WHITE);
		rbMaxRecords.setOpaque(false);
		
		rbAllRecords.setBounds(Math.round(33 * window.xScale), Math.round(100 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		rbAllRecords.setFont(font);
		rbAllRecords.setForeground(Color.WHITE);
		rbAllRecords.setOpaque(false);
		
		lSearch.setBounds(Math.round(25 * window.xScale), Math.round(150 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		lSearch.setFont(font);
		lSearch.setForeground(Color.WHITE);
				
		tfSearch.setBounds(Math.round(150 * window.xScale), Math.round(150 * window.yScale),
				Math.round(120 * window.xScale), Math.round(25 * window.yScale));
		tfSearch.setFont(font);
		
		bSearch.setBounds(Math.round(25 * window.xScale), Math.round(175 * window.yScale), 
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		bSearch.setFont(font);
		
		bBack.setBounds(Math.round(25 * window.xScale), Math.round(625 * window.yScale),
				Math.round(100 * window.xScale), Math.round(25 * window.yScale));
		bBack.setFont(font);
		
		//Setting Background
		Image image = logic.Utilities.loadImage("res/EarthDefenderBackground.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/EarthDefenderBackground.png");
		}
		background.setIcon(new ImageIcon(image));
		background.setBounds(0, 0, image.getWidth(null), image.getHeight(null));
		
		this.add(lShow);
		this.add(cbOnlyMines);
		this.add(rbMaxRecords);
		this.add(rbAllRecords);
		this.add(lSearch);
		this.add(tfSearch);
		this.add(bSearch);
		this.add(bBack);
		this.add(tablePanel);
		this.add(background);
		
		cbOnlyMines.addItemListener(this);
		rbMaxRecords.addItemListener(this);
		rbAllRecords.addItemListener(this);
		bSearch.addActionListener(this);
		bSearch.addMouseListener(this);
		bBack.addActionListener(this);
		bBack.addMouseListener(this);
		
		tfSearch.requestFocus();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(bSearch)) {
			if(tfSearch.getText().length() > 0) {
				for(int i = 0; i < model.getRowCount(); i++) {
					if(tfSearch.getText().equals(model.getValueAt(i, 1))) {
						showSearchResult(i, 1);
						return;
					}
				}
			}
		}else if(e.getSource().equals(bBack)) {
			window.add(new SpaceInvadersMainMenuPanel(window));
			this.setVisible(false);
			window.remove(this);
		}
	}
	
	public void loadModel(ArrayList<Player> list) {
		for (int i = 0; i < list.size(); i++) {
			Player temp = list.get(i);
			model.addRow(new Object[] {i+1, temp.getName(), temp.getScore() });
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		for(int i = model.getRowCount() - 1; i >= 0; i--) {
			model.removeRow(i);
		}
		if(e.getSource().equals(cbOnlyMines)) {
			if(cbOnlyMines.isSelected()) {
				lSearch.setVisible(false);
				tfSearch.setVisible(false);
				bSearch.setVisible(false);
				if(rbMaxRecords.isSelected()) {
					ArrayList<Player> player = new ArrayList<Player>();
					player.add(database.Search.playerMaxRecord(window.userName, GameTables.SPACEINVADERS));
					loadModel(player);
				} else if(rbAllRecords.isSelected()) {
					loadModel(database.Search.allPlayerRecord(window.userName, GameTables.SPACEINVADERS));
				}
			} else {
				lSearch.setVisible(true);
				tfSearch.setVisible(true);
				bSearch.setVisible(true);
				if(rbMaxRecords.isSelected()) {
					loadModel(database.Search.playersMaxRecord(GameTables.SPACEINVADERS));
				} else if(rbAllRecords.isSelected()) {
					loadModel(database.Search.allPlayerRecord(GameTables.SPACEINVADERS));
				}
			}
		}else if(e.getSource().equals(rbMaxRecords)) {
			if(cbOnlyMines.isSelected()) {
				ArrayList<Player> player = new ArrayList<Player>();
				player.add(database.Search.playerMaxRecord(window.userName, GameTables.SPACEINVADERS));
				loadModel(player);
			} else {
				loadModel(database.Search.playersMaxRecord(GameTables.SPACEINVADERS));
			}
		} else if(e.getSource().equals(rbAllRecords)) {
			if(cbOnlyMines.isSelected()) {
				loadModel(database.Search.allPlayerRecord(window.userName, GameTables.SPACEINVADERS));
			} else {
				loadModel(database.Search.allPlayerRecord(GameTables.SPACEINVADERS));
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if(e.getSource().equals(bSearch)) {
			Utilities.buttonEffect(bSearch, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bBack)) {
			Utilities.buttonEffect(bBack, 5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		if(e.getSource().equals(bSearch)) {
			Utilities.buttonEffect(bSearch, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bBack)) {
			Utilities.buttonEffect(bBack, -5, window.xScale, window.yScale);
		}
	}

	private void showSearchResult(int row, int col) {
		if(tLeaderBoard.getSelectedRow() != -1) {
			tLeaderBoard.clearSelection();
		}
		tLeaderBoard.getSelectionModel().setSelectionInterval(row, row);
		Rectangle r = tLeaderBoard.getCellRect(row, col, false);  
        tLeaderBoard.scrollRectToVisible(r);  
        tLeaderBoard.repaint();  
		
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
}
